﻿using AutoMapper;
using B.S.Traceable.Domain.Entitiy;
using B.S.Traceable.ErrorCode;
using B.S.Traceable.Infrasturcture.Interfaces;
using B.S.Traceable.Write.Api.Application.Command.Traceable;
using MediatR;

namespace B.S.Traceable.Write.Api.Application.CommandHandler.Traceable
{
    public class UpdateTraceableHandler : IRequestHandler<UpdateTraceableCommand, ApiResult<object>>
    {

        private readonly IBaseRepository<TraceableModel> tracRepo;
        private readonly IBaseRepository<ShopModel> shopRepo;
        private readonly IMapper mapper;

        public UpdateTraceableHandler(IBaseRepository<TraceableModel> tracRepo, IBaseRepository<ShopModel> shopRepo, IMapper mapper)
        {
            this.tracRepo = tracRepo;
            this.shopRepo = shopRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<object>> Handle(UpdateTraceableCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            var model=tracRepo.GetAll().FirstOrDefault(x=>x.TraceableId==request.TraceableId);
            model.TraceableId = request.TraceableId;
            model.ShopId = request.ShopId;
            model.Production_batches=request.Production_batches;
            model.Shelf_Life=request.Shelf_Life;
            model.Production_License_Number=request.Production_License_Number;
            model.Production_Industry=request.Production_Industry;
            model.Picture = request.Picture;
            model.Information="0%";
            model.CreateName="张三";
            model.CreateTime=DateTime.Now;
            model.UpdateName="李四";
            model.UpdateTime=DateTime.Now;

            var s=tracRepo.Update(model).Result;
            if (s > 0)
            {
                result.Code = ApiEnums.Success;
                result.Msg = "修改成功";
                result.Data = s;
            }
            else
            {
                result.Code = ApiEnums.Failed;
                result.Msg = "修改失败";
                result.Data = s;
            }
            return Task.FromResult(result);

        }
    }
}
